﻿using XinLife.Application.Interface;
using XinLife.Common;
using XinLife.Core;
using XinLife.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using XinLife.Core.Model;
using XinLife.Application.Implement;

namespace XinLife.Controllers.Admin
{
    /// <summary>
    /// 地址管理
    /// </summary>
    public class AddressController : BaseController
    {
        IAddressService _addressService;
        public AddressController(IAddressService addressService)
        {
            _addressService = addressService;
        }

        /// <summary>
        /// 获取区划下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AjaxResult> GetRegion(int type, string regionCode = null)
        {
            if (type == -1) //新增时
            {
                return Success(new { sheng = await _addressService.GetRegion("", 1) });
            }
            else if (type == 0) //编辑时
            {
                return Success(new { sheng = await _addressService.GetRegion("", 1), shi = await _addressService.GetRegion(regionCode, 2, 1), qu = await _addressService.GetRegion(regionCode, 3, 2) });
            }
            else if (type == 1)//选择省获取市
            {
                return Success(new { shi = await _addressService.GetRegion(regionCode, 2, 3) });
            }
            else if (type == 2)//选择市获取区
            {
                return Success(new { qu = await _addressService.GetRegion(regionCode, 3, 3) });
            }
            return Error("空");
        }


        /// <summary>
        /// 获取区划列表
        /// </summary>
        [HttpGet]
        public async Task<AjaxResult> GetAllRegions(string cRegionCode)
        {
            var rlist = await _addressService.GetAllRegions();
           
            Func<List<Region>, string, List<object>> res = null;
            //递归0.0
            res = (List<Region> list, string parentid) =>
            {
                var items = list.FindAll(t => t.cParentId == parentid).ToList();
                var b = new List<object>();

                items.ForEach(t =>
                {
                    b.Add(new
                    {
                        Id = t.cId,
                        spread = true,
                        ParentId = parentid,
                        Title = t.cRegionName,
                        Code = t.cRegionCode,
                        cLevel = t.cLevel,
                        children = res(list, t.cId),
                        value = t.cRegionCode,
                        name = t.cRegionName,
                        selected = t.cRegionCode == cRegionCode,
                    });
                });
                return b;
            };

            return Success(res(rlist, "0"));
        }
        /// <summary>
        /// 获取功能模块列表
        /// </summary>
        [HttpGet]
        public async Task<object> GetRegionPageList([FromQuery] Pagination pagination, [FromQuery] string queryJson = null)
        {
            return await _addressService.GetRegionPageList(pagination, queryJson);
        }
        /// <summary>
        /// 获取所有区划 
        /// </summary>
        /// <param name="cId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AjaxResult> GetAllAddress(string cParentId)
        {
            var rlist = await _addressService.GetAllRegions();

            Func<List<Region>, string, List<object>> res = null;
            //递归0.0
            res = (List<Region> list, string parentid) =>
            {
                var items = list.FindAll(t => t.cParentId == parentid).ToList();
                var b = new List<object>();

                items.ForEach(t =>
                {
                    b.Add(new
                    {
                        value = t.cId,
                        selected = t.cId == cParentId,
                        cParentId = parentid,
                        name = t.cRegionName,
                        cRegionCode = t.cRegionCode,
                        cLevel = t.cLevel,
                        children = res(list, t.cId)
                    });
                });
                return b;
            };
            return Success(res(rlist, "0"));
        }

        /// <summary>
        /// 获取表单信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AjaxResult> GettAddressForm(string id)
        {
            return await _addressService.GettAddressForm(id);
        }

        /// <summary>
        /// 编辑区划
        /// </summary>-
        /// <returns></returns>
        [HttpPost]
        public async Task<AjaxResult> EditAddress([FromForm] Region region)
        {
            return await _addressService.EditRegion(region);
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost]
        public async Task<AjaxResult> DelAddress([FromForm] string ids)
        {
            return await _addressService.DelAddress(ids);
        }
    }

}
